<template>
  <div class="block" v-if="hunterInfo.schemaCodeAuth">
    <div v-if="realSchemaList.length === 0" class="hunter-schema-none">暂无收藏的数据源，请先选择数据源</div>
    <ep-tabs v-else v-model="activeTab">
      <ep-tab-item v-for="schema in realSchemaList" :name="schema" :label="showSchemaLabel(schema)" style="position: relative">
        <div class="action-block">
          <ep-button v-if="dbType === 'mongodb' || checkAuthCr(schema)" size="mini" type="primary" @click="openQuery(schema)">自定义查询</ep-button>
          <ep-button v-if="dbType !== 'mongodb' && checkAuthRls(schema)" size="mini" type="success" @click="openRls(schema)">标准变更</ep-button>
          <ep-button size="mini" type="danger" @click="delSchemaSearch(schema)">关闭</ep-button>
        </div>
        <ul class="hunter-intro-block">
          <li class="hunter-intro-item">
            <div class="hunter-intro-item-title">{{ dbType === 'mongodb' ? 'Mongo' : 'SQL' }}配置数量：</div>
            <div class="hunter-intro-item-label word-one-line">{{ schemaData.length }} 条</div>
          </li>
          <li class="hunter-intro-item-tab" style="height: 140px">
            <template v-if="schemaData.length > 0">
              <ep-tag v-for="(item, index) in schemaData" :key="index" type="primary" style="margin: 0 3px" @click.native="handleTagClick(item)"
                >{{ item.meno }}
              </ep-tag>
            </template>
            <div v-else style="color: #546e7a; padding-left: 10px">暂无数据</div>
          </li>
        </ul>
      </ep-tab-item>
    </ep-tabs>
  </div>
</template>

<script>
import { getHunterAuth, getHunterSchema } from './api'

export default {
  name: 'hunterWorkSchemaTab',

  props: {
    dbType: String,
    schemaSearchList: Array
  },

  computed: {
    realSchemaList() {
      let dbType = this.dbType
      let realSchemaList = this.schemaSearchList.filter(item => {
        if (dbType === 'mysql') {
          return item.startsWith('mysql.')
        } else if (dbType === 'mongodb') {
          return item.startsWith('mongodb.')
        } else if (dbType === 'dameng') {
          return item.startsWith('dameng.')
        } else if (dbType === 'oracle') {
          if (!item.startsWith('mysql.') && !item.startsWith('mongodb.') && !item.startsWith('dameng.')) return item
        } else {
          return !item.startsWith('mysql.') && !item.startsWith('mongodb.')
        }
      })
      if (realSchemaList.length > 0) {
        this.activeTab = realSchemaList[0]
      }
      return realSchemaList
    }
  },
  created() {
    this.init()
  },
  watch: {
    activeTab(val) {
      this.handleGetSchema(val)
    }
  },

  data() {
    return {
      activeTab: '',
      schemaData: [],
      hunterInfo: {}
    }
  },

  methods: {
    init() {
      let vm = this
      let token = this.$store.getters.getToken
      getHunterAuth(token)
        .then(json => {
          console.log('11111',json);
          if (json.data.flag === 'T') {
            let { data } = json.data
            let hunterAuthData = JSON.parse(data)
            this.hunterInfo = hunterAuthData
          } else {
          }
        })
        .catch(e => {
        })
    },
    checkAuthCr(schema) {
      let { schemaCodeAuth } = this.hunterInfo
      return (schemaCodeAuth[schema] || {}).crudAuth.indexOf('CR') !== -1
    },
    checkAuthRls(schema) {
      let { groupList, schemaCodeAuth } = this.hunterInfo
      if (groupList.indexOf('HUNTER_RX') !== -1) {
        return false
      }
      let rlsSubmitList = []
      for (let key in schemaCodeAuth) {
        let rlsAuth = schemaCodeAuth[key].rlsAuth
        if (rlsAuth.indexOf('SC_SUBMIT') !== -1) {
          rlsSubmitList.push(key)
        }
      }

      if (schema) {
        return rlsSubmitList.indexOf(schema) !== -1
      }
    },
    handleGetSchema(schema) {
      getHunterSchema(schema).then(json => {
        let { data } = json
        this.schemaData = data || []
      })
    },
    showSchemaLabel(schema) {
      let dbType = this.dbType
      if (dbType === 'mysql') {
        return schema.replace('mysql.', '')
      } else if (dbType === 'mongodb') {
        return schema.replace('mongodb.', '')
      } else {
        return schema
      }
    },
    openRls(schema) {
      this.$app.trigger('chunk-tab', 'hbRls', '新增变更 - ' + schema)
    },
    openQuery(schema) {
      // if (this.dbType === 'mongodb') {
      //   this.$app.trigger('locate-tab', 'hbMongo', schema.replace('mongodb.', ''))
      // } else {
      //   this.$app.trigger('locate-tab', 'hbQuery', schema)
      // }
      if (this.dbType === 'mongodb') {
        this.$app.trigger('chunk-tab', 'hbMongo', schema.replace('mongodb.', ''))
      } else {
        this.$app.trigger('chunk-tab', 'hbQuery', schema)
      }
    },
    delSchemaSearch(schema) {
      this.$emit('close', schema)
    },
    handleTagClick(row) {
      // this.$app.trigger("locate-tab", "hbCommon", {
      //   id: row.meno + " - " + row.id,
      //   realId: row.id,
      // });
      this.$app.trigger('chunk-tab', 'hunterPageExec', {
        id: row.meno + ' - ' + row.id,
        realId: row.id
      })
    }
  }
}
</script>

<style lang="less" scoped>
.hunter-schema-none {
  width: 100%;
  height: 250px;
  text-align: center;
  padding-top: 120px;
  color: #546e7a;
}

.action-block {
  position: absolute;
  overflow: hidden;
  z-index: 50;
  right: 10px;
}
.block{
  height: 550px;
}
</style>
